package com.potenza.training.dictionary.controller;

import com.potenza.training.dictionary.entity.SubjectDifficultyEntity;
import com.potenza.training.dictionary.server.SubjectDifficultyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 难度表
 *
 * @author lihonggang
 * @email lihonggang@gmail.com
 * @date 2021-07-22 15:57:42
 */
@RestController
@RequestMapping("/hope/reply/subjectdifficulty")
public class SubjectDifficultyController {

    @Autowired
    private SubjectDifficultyService subjectDifficultyService;

    static List<SubjectDifficultyEntity> subjectDifficultyEntityList = new ArrayList<SubjectDifficultyEntity>();

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("reply:subjectdifficulty:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = subjectDifficultyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("reply:subjectdifficulty:info")
    public R info(@PathVariable("id") Long id){
		SubjectDifficultyEntity subjectDifficulty = subjectDifficultyService.getById(id);

        return R.ok().put("subjectDifficulty", subjectDifficulty);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("reply:subjectdifficulty:save")
    public R save(@RequestBody SubjectDifficultyEntity subjectDifficulty){
		subjectDifficultyService.save(subjectDifficulty);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("reply:subjectdifficulty:update")
    public R update(@RequestBody SubjectDifficultyEntity subjectDifficulty){
		subjectDifficultyService.updateById(subjectDifficulty);

        return R.ok();
    }

    @GetMapping("/findByPid/{pid}")
    public List<SubjectDifficultyEntity> findDifficultyByPid(@PathVariable int pid){
        return subjectDifficultyService.findByPid(pid);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("reply:subjectdifficulty:delete")
    public R delete(@RequestBody Long[] ids){
		subjectDifficultyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取所有的题目难度
     * @return
     */
    @GetMapping("/all")
    public List<SubjectDifficultyEntity> findAll(){
        return subjectDifficultyService.findAll();
    }

    /**
     * 获取经过处理的项目类型
     * @return
     */
    @GetMapping("/alls")
    public List<SubjectDifficultyEntity> edit(){
        subjectDifficultyEntityList.clear();
        List<SubjectDifficultyEntity> all = findAll();
        for (SubjectDifficultyEntity subjectDifficultyEntity : all) {
            subjectDifficultyEntityList.add(new SubjectDifficultyEntity(subjectDifficultyEntity.getId(),subjectDifficultyEntity.getDifficultyName(),subjectDifficultyEntity.getPid(),null));
        }

        List<SubjectDifficultyEntity> collect = subjectDifficultyEntityList.stream().filter(subjectDifficultyEntity ->
                subjectDifficultyEntity.getPid() == 0
        ).map((menu) -> {
            menu.setChildren(getChildrens(menu, subjectDifficultyEntityList));
            return menu;
        }).collect(Collectors.toList());

        return collect;
    }

    //递归查找所有菜单的子类菜单
    private static List<SubjectDifficultyEntity> getChildrens(SubjectDifficultyEntity root,List<SubjectDifficultyEntity> all){
        List<SubjectDifficultyEntity> children = all.stream().filter(linkageEntities -> Long.parseLong(String.valueOf(linkageEntities.getPid()))==root.getId()).map(linkageEntities -> {
            //1、递归找到子菜单
            linkageEntities.setChildren(getChildrens(linkageEntities,all));
            return linkageEntities;
        }).collect(Collectors.toList());
        return children;
    }
}
